package com.gewu.bookeeping.View.ItemFragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.gewu.bookeeping.IModel.IMenyInDaoModel;
import com.gewu.bookeeping.MyDb.bean.ALIBeanIn;
import com.gewu.bookeeping.MyDb.bean.ALiBeanOut;
import com.gewu.bookeeping.MyDb.bean.MesBeanOut;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanOut;
import com.gewu.bookeeping.MyDb.dao.MesOutPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoOutPerson;
import com.gewu.bookeeping.R;
import com.gewu.bookeeping.Utils.Iperson;
import com.gewu.bookeeping.Utils.MyNotiListion;
import com.gewu.bookeeping.View.activity.ItemActivity;
import com.gewu.bookeeping.View.adapter.OneItemAdapter;

import java.util.List;

public class oneItem1Fragment extends Fragment implements IMenyInDaoModel.ICallBack, com.gewu.bookeeping.OnClick.goBack {

    private ExpandableListView listView;
    private OneItemAdapter oneItemAdapter;
    private WeChtDaoInPerson weChtDaoInPerson;
    private WeChtDaoOutPerson weChtDaoOutPerson;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_one_item1_fragment, container, false);
        initview(inflate);
        initAdapter();
        initShuju();
        initShuju2();
        return inflate;
    }

    /**
     * 当第一次进入页面时 查询所有数据并且显示
     */
    private void initShuju() {
        //收入集合
        List<WeChtBeanIn> quer = weChtDaoInPerson.quer();
        for (int i = 0; i < quer.size(); i++) {
            oneItemAdapter.text2Add(quer.get(i));
        }
        oneItemAdapter.notifyDataSetChanged();
    }


    /**
     * 当第一次进入页面时 查询所有数据并且显示
     */
    private void initShuju2() {
        //收入集合
        List<WeChtBeanOut> quer = weChtDaoOutPerson.quer();
        for (int i = 0; i < quer.size(); i++) {
            oneItemAdapter.text1Add(quer.get(i));
        }
        oneItemAdapter.notifyDataSetChanged();
    }

    private void initAdapter() {
        oneItemAdapter = new OneItemAdapter(this);
        listView.setAdapter(oneItemAdapter);
    }

    /**
     * 初始化适配器
     *
     * @param inflate
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void initview(View inflate) {
        listView = inflate.findViewById(R.id.EtListView);
        weChtDaoInPerson = new WeChtDaoInPerson(getContext());
        weChtDaoOutPerson = new WeChtDaoOutPerson(getContext());
        MyNotiListion.getInstance(this);
    }


    /**
     * 自定义接口 当接收到信息时通知UI更新数据
     */
    @Override
    public void CallBack() {
        initShuju();
        initShuju2();
    }


    @Override
    public void onItemClieck(int index, int type) {
        Intent intent = new Intent(getActivity(), ItemActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("values", index);
        bundle.putInt("type", type);
        intent.putExtra("date", bundle);
        startActivity(intent);
    }

    @Override
    public <E> void longItemClick(E e, final int groupPosition) {
        String text;
        WeChtBeanIn e2 = null;
        WeChtBeanOut e1 = null;
        if (groupPosition == 0) {
            e1 = (WeChtBeanOut) e;
            text = e1.getText();
        } else {
            e2 = (WeChtBeanIn) e;
            text = e2.getText();
        }

        final WeChtBeanOut finalE = e1;
        final WeChtBeanIn finalE1 = e2;

        AlertDialog alertDialog = new AlertDialog.Builder(getContext())
                .setTitle("收藏")//标题
                .setMessage("您即将收藏" + text + "这条消息！")//内容
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MesOutPerson mesOutPerson = new MesOutPerson(getContext());
                        if (groupPosition == 0) {
                            MesBeanOut mesBeanOut = new MesBeanOut();
                            mesBeanOut.setText(finalE.getText());
                            mesBeanOut.setTime(finalE.getTime());
                            mesBeanOut.setMoney(finalE.getMoney());
                            mesOutPerson.add(mesBeanOut);
                        } else {
                            MesBeanOut mesBeanOut = new MesBeanOut();
                            mesBeanOut.setText(finalE1.getText());
                            mesBeanOut.setTime(finalE1.getTime());
                            mesBeanOut.setMoney(finalE1.getMoney());
                            mesOutPerson.add(mesBeanOut);
                        }
                        dialog.dismiss();
                    }
                })
                .create();
        alertDialog.show();
    }


}